﻿#region File Description
//-----------------------------------------------------------------------------
// Particle.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Collision;
using D2DEngine.Util;
#endregion

namespace D2DEngine.ParticleEffect
{
    public class D2DParticle
    {
        bool isAlive = false;

        private float lifetime;
        private float timeSinceStart;
        private float scale;
        private float rotationSpeed;

        private Vector2 velocity;
        private Vector2 acceleration;

        private Vector2 position;
        private float rotation;

        /// <summary>
        /// Is the particle alive ?
        /// </summary>
        public bool IsAlive
        {
            get { return this.isAlive; }
            set { this.isAlive = value; }
        }

        /// <summary>
        /// The position of the particle
        /// </summary>
        public Vector2 Position
        {
            get { return this.position; }
            set { this.position = value; }
        }        

        /// <summary>
        /// The orientation of the particle
        /// </summary>
        public float Rotation
        {
            get { return this.rotation; }
            set { this.rotation = value; }
        }

        /// <summary>
        /// The velocity of the particle
        /// </summary>
        public Vector2 Velocity
        {
            get { return this.velocity; }
            set { this.velocity = value; }
        }

        /// <summary>
        /// The acceleration of the particle
        /// </summary>
        public Vector2 Acceleration
        {
            get { return this.acceleration; }
            set { this.acceleration = value; }
        }
        
        /// <summary>
        /// The life time of the particle
        /// </summary>
        public float Lifetime
        {
            get { return lifetime; }
            set { lifetime = value; }
        }

        /// <summary>
        /// The time since the particle has been initialized
        /// </summary>
        public float TimeSinceStart
        {
            get { return timeSinceStart; }
            set { timeSinceStart = value; }
        }

        /// <summary>
        /// The scale of the particle
        /// </summary>
        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        /// <summary>
        /// The rotation speed of the particle
        /// </summary>
        public float RotationSpeed
        {
            get { return rotationSpeed; }
            set { rotationSpeed = value; }
        }

        public D2DParticle()
        {
        }

        /// <summary>
        /// Initializes the particle
        /// </summary>
        /// <param name="position"></param>
        /// <param name="velocity"></param>
        /// <param name="acceleration"></param>
        /// <param name="lifetime"></param>
        /// <param name="scale"></param>
        /// <param name="rotationSpeed"></param>
        public void Initialize(Vector2 position, Vector2 velocity, Vector2 acceleration,
            float lifetime, float scale, float rotationSpeed)
        {
            this.position = position;
            this.velocity = velocity;

            this.acceleration = acceleration;
            this.Lifetime = lifetime;
            this.Scale = scale;
            this.RotationSpeed = rotationSpeed;

            this.TimeSinceStart = 0.0f;

            this.Rotation = D2DMath.RandomFloat(0, MathHelper.TwoPi);

            this.isAlive = true;
        }

        /// <summary>
        /// Updates the particle
        /// </summary>
        /// <param name="dt"></param>
        public void Update(float dt)
        {
            if (TimeSinceStart > Lifetime)
            {
                this.isAlive = false;
            }

            this.velocity += this.acceleration * dt;
            this.position += this.velocity * dt;

            Rotation += RotationSpeed * dt;

            TimeSinceStart += dt;
        }
    }
}
